package com.xqboss.chain.domain.chain.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.PageHelper;
import com.xqboss.chain.domain.chain.UpUpdateChainLog;
import com.xqboss.common.utils.StringUtils;
import com.xqboss.common.utils.sql.SqlUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.Max;
import java.util.Objects;

/**
 * @author Trent
 * @date 2023/9/4
 */
@Data
@ApiModel
public class GetUpChainLogListDto extends UpUpdateChainLog {

    /**
     * 当前记录起始索引
     */
    @ApiModelProperty(value = "当前页", example = "1")
    private Integer pageNum = 1;

    /**
     * 每页显示记录数
     */
    @ApiModelProperty(value = "每页记录数", example = "10")
    @Max(value = 30, message = "最大支持每页30条")
    private Integer pageSize = 10;

    @ApiModelProperty(value = "排序字段")
    private String orderByColumn;

    @ApiModelProperty(value = "排序方式")
    private Boolean sortAsc;


    @JsonIgnore
    public String buildOrderBy(){
        if(StringUtils.isEmpty(orderByColumn)){
            return "";
        }
        if(Objects.isNull(sortAsc)){
            return StringUtils.toUnderScoreCase(orderByColumn);
        }else{
            return StringUtils.toUnderScoreCase(orderByColumn) + " " + (sortAsc ? "asc" : "desc");
        }
    }

    @JsonIgnore
    public void startPage(){
        PageHelper.startPage(pageNum, pageSize);
    }

    @JsonIgnore
    public void startOrder(){
        String orderBy = buildOrderBy();
        if(StringUtils.isNotEmpty(orderBy)){
            PageHelper.orderBy(SqlUtil.escapeOrderBySql(orderBy));
        }
    }

    @JsonIgnore
    public void startPageAndOrder(){
        startPage();
        startOrder();
    }
}
